package com.example.room.repository;

import android.content.Context;
import android.os.AsyncTask;

import com.example.room.dao.TeamDao;
import com.example.room.database.MyDatabase;
import com.example.room.entity.Team;
import com.example.wjy.room.NativeDatabase;

import java.util.List;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;

/**
 * author: WangJunYan
 * package: com.example.wjy.room
 * date: 2020/6/19
 * time: 13:52
 * 通过此类访问对应的Dao，在子线程中访问数据库
 * 目前的方法只是测试是否可以加载
 */
public class TeamRepository {

    private LiveData<List<Team>> teams;

    private TeamDao teamDao;

    public TeamRepository(Context context) {
        MyDatabase database = MyDatabase.getInstance(context.getApplicationContext());
        teamDao = database.getTeamDao();
        teams = teamDao.getAllTeam();
    }

    public LiveData<List<Team>> getTeams() {
        return teams;
    }

    public void insertTeam(Team... teams){
        new InsertTeamAsyncTask(teamDao).execute(teams);
    }

    //内部类，用于在子线程中访问数据库，如果有多个操作的话，每一个操作都要对应一个AsyncTask
    static class InsertTeamAsyncTask extends AsyncTask<Team,Void,Void> {
        private TeamDao teamDao;

        public InsertTeamAsyncTask(TeamDao teamDao) {
            this.teamDao = teamDao;
        }

        @Override
        protected Void doInBackground(Team... teams) {
            teamDao.insertTeam(teams);
            return null;
        }
    }

    public void deleteTeam(Team... teams) {
        new DeleteTeamAsyncTask(teamDao).execute(teams);
    }

//    public void deleteTeam(long id) {
//        new DeleteTeamAsyncTask(teamDao).doInBackground(id);
//    }

    static class DeleteTeamAsyncTask extends AsyncTask<Team,Void,Void> {
        private TeamDao teamDao;
        public DeleteTeamAsyncTask(TeamDao teamDao) {
            this.teamDao = teamDao;
        }

        @Override
        protected Void doInBackground(Team... teams) {
            teamDao.deleteTeam(teams);
            return null;
        }

    }

//    static class DeleteTeamAsyncTaskById extends AsyncTask<Long,Void,Void> {
//        private TeamDao teamDao;
//        public DeleteTeamAsyncTaskById(TeamDao teamDao) {
//            this.teamDao = teamDao;
//        }
//
//
//        @Override
//        protected Void doInBackground(Long... longs) {
//            return null;
//        }
//    }

    public void modifyTeam(Team... teams) {
        new ModifyTeamAsyncTask(teamDao).execute(teams);
    }

    static class ModifyTeamAsyncTask extends AsyncTask<Team,Void,Void> {
        private TeamDao teamDao;

        public ModifyTeamAsyncTask(TeamDao teamDao) {
            this.teamDao = teamDao;
        }

        @Override
        protected Void doInBackground(Team... teams) {
            teamDao.modify(teams);
            return null;
        }
    }

    public void deleteAllTeam() {
        new DeleteAllTeamAsyncTask(teamDao).execute();
    }

    static class DeleteAllTeamAsyncTask extends AsyncTask<Void,Void,Void> {

        TeamDao teamDao;

        public DeleteAllTeamAsyncTask(TeamDao teamDao) {
            this.teamDao = teamDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            teamDao.deleteTeam();
            return null;
        }
    }
}
